<?php
/**
 * 
 * Application for managing system users
 * 
 * @category Abovo
 * 
 * @package Abovo_App_System
 * 
 * @subpackage Abovo_App_System_Users
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 */

/**
 * 
 * Application for managing system users
 *
 * @category Abovo
 *
 * @package Abovo_App_System
 * 
 * @subpackage Abovo_App_System_Users
 * 
 */
class Abovo_App_System_Users extends Abovo_App_System {
    
    /**
     * 
     * User information
     * 
     * @var Solar_Sql_Row
     * 
     */
    public $user;
    
    /**
     * 
     * Whole menu structure available
     * 
     * @var array
     * 
     */
    public $perms = array();
    
    /**
     * 
     * List of system users
     * 
     * @var Solar_Sql_Rowset
     * 
     */
    public $users;
    
    /**
     * 
     * List of sent invitations
     * 
     * @var Solar_Sql_Rowset
     * 
     */
    public $invitations;
    
    /**
     * 
     * Form object for invite sending
     * 
     * @var Solar_Form
     * 
     */
    public $invite_form;
    
    /**
     * 
     * Instance of invite domain model
     * 
     * @var Abovo_Invite
     * 
     */
    protected $_invite;
    
    /**
     * 
     * Default action
     * 
     * @var string
     * 
     */
    protected $_action_default = 'browse';
    
    /**
     * 
     * Shows a list of users
     * 
     * @return void
     * 
     */
    public function actionBrowse()
    {
        // get users model
        $users = Solar::factory('Abovo_Model_Contacts_Users');
        
        // just get all system users
        // @todo add paging/search, this list will get big
        $this->users = $users->fetchAll();
    }
    
    /**
     * 
     * Change user permissions
     * 
     * @param string $handle User handle
     * 
     * @return void
     * 
     */
    public function actionPermissions($handle = null)
    {
        // get users model
        $users = Solar::factory('Abovo_Model_Contacts_Users');
        
        $this->user = $users->fetchByHandle($handle);
        
        // does this handle exist?
        if (! $this->user) {
            $this->_redirect('system/users/users');
        }
        
        // get menu object
        $menu = Solar::registry('menu');
        
        if ($this->_isProcess('save')) {
            $apps = $this->_request->post('apps', array());
            
            // gather all the perms that need to be added
            $data = array();
            foreach ($apps as $app => $actions) {
                foreach ($actions as $action => $chosen) {
                    if ($chosen === '1') {
                        $data[] = array(
                            'class'  => $app,
                            'action' => $action,
                        );
                    }
                }
            }
            
            // refresh permissions
            $menu->perms->refresh($handle, $data);
        }
        
        // get access list for this handle
        $access = Solar::factory('Solar_Access');
        $access->load($handle, array());
        
        // assign apps for view
        $this->perms = $menu->menu;
        
        // go through and set checked
        foreach ($this->perms as &$module) {
            foreach ($module['apps'] as $class => &$app) {
                foreach ($app['actions'] as $action => &$info) {
                    $info['value'] = 0;
                    if ($access->isAllowed($class, $action)) {
                        $info['value'] = 1;
                    }
                }
            }
        }
    }
    
    /**
     * 
     * Send and manage user invitations
     * 
     * @return void
     * 
     */
    public function actionInvitations()
    {
        // get invitation form
        $form = $this->_invite->form();
        
        if ($this->_isProcess('go') && $form->validate()) {
            $values = $form->values();
            
            // create and send invitation
            $this->_invite->send($values['email'], $values['message']);
            
            // add message for view
            $this->_msgSuccess($this->locale('TEXT_INVITATION_SENT'));
            
        } else {
            $form->populate();
        }
        
        // set form object for view
        $this->invite_form = $form;
        
        // invitations
        $this->invitations = $this->_invite->invites->fetchAll(null, 'created DESC');
    }
    
    /**
     * 
     * Setup
     * 
     * @return void
     * 
     */
    public function _setup()
    {
        parent::_setup();
        
        // set invite domain model
        $this->_invite = Solar::factory('Abovo_Invite');
        
        // add these to the locale nav
        $this->layout_local['system/users/browse'] = $this->locale(
            'APP_ACTION_BROWSE'
        );
        
        $this->layout_local['system/users/invitations'] = $this->locale(
            'APP_ACTION_INVITATIONS'
        );
    }
}
